# coding: utf8
import os
#########################################################################
## If you need to use OpenID, Facebook, MySpace, Twitter, Linkedin, etc.
## register with janrain.com, uncomment and customize following
# from gluon.contrib.login_methods.rpx_account import RPXAccount
# auth.settings.actions_disabled=['register','change_password','request_reset_password']
# auth.settings.login_form = RPXAccount(request, api_key='...',domain='...',
#    url = "http://localhost:8000/%s/default/user/login" % request.application)
## other login methods are in gluon/contrib/login_methods
#########################################################################
from gluon.contrib.login_methods.rpx_account import RPXAccount
from gluon.contrib.login_methods.extended_login_form import ExtendedLoginForm

janrain_api_key_path = os.path.join(request.folder,'private','janrain_api_key.txt')
# janrain_target_domain_path = os.path.join(request.folder,'private','janrain_target_domain.txt')
janrain_target_domain = 'voiceofaccess'
url = "http://%s.org/init/plugin_uc_janrain/user/login" % janrain_target_domain


if not os.path.exists(janrain_api_key_path):
    '''
    if keyfile is found, we create an empty one.
    '''
    our_file = open(janrain_api_key_path,"w")
    our_text = 'visit http://www.janrain.com/ to get a janrain api key for your domain'
    our_file.writelines(our_text)
    our_file.close()


default_login_form = auth.settings.login_form

# define where to go after RPX login
if request.vars._next:
#    url = "http://yourappdomain/init/default/user/login?_next=%s" % request.vars._next
#    url = "http://127.0.0.1:8000/a_test/default/user/login?_next=%s" % request.vars._next
#    url = "http://voiceofaccess.org/init/default/user/login?_next=%s" % request.vars._next
#    url = "http://voicesofaccess.org/init/plugin_uc_janrain/user/login?_next=%s" % request.vars._next
    url += "?_next=%s" % request.vars._next


else:
#    url = "http://yourappdomain/init/default/user/login"
#    url = "http://127.0.0.1:8000/a_test/default/user/login"
#    url = "http://voiceofaccess.org/init/default/user/login"
#    url = "http://voicesofaccess.org/init/plugin_uc_janrain/user/login"
    url = url
   
janrain_api_key = open(os.path.join(request.folder,'private','janrain_api_key.txt'),'r').read().strip()
janrain_login_form = RPXAccount(request, 
    api_key=janrain_api_key,
    domain='%s' % janrain_target_domain,
    url = url,
    language="english",
    embed=True
    )
